<?php
App::uses('AppController', 'Controller');
/**
 * Categorias Controller
 *
 * @property Categoria $Categoria
 * @property PaginatorComponent $Paginator
 */
class CategoriasController extends AppController {

/**
 * Components
 *
 * @var array
 */
    public $layout = 'inventario';
	public $components = array('Paginator');

/**
 * index method
 *
 * @return void
 */
	public function index() {
		//$this->Categoria->recursive = 0;
		//$this->set('categorias', $this->Paginator->paginate());
        
        $categorias = $this->Categoria->find('all', array('order'=>'Categoria.id DESC'));
        $this->set(compact('categorias'));
	}

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Categoria->exists($id)) {
			throw new NotFoundException(__('Invalid categoria'));
		}
		$options = array('conditions' => array('Categoria.' . $this->Categoria->primaryKey => $id));
		$this->set('categoria', $this->Categoria->find('first', $options));
	}

/**
 * add method
 *
 * @return void
 */
	public function add(){
	   if(!empty($this->request->data)){
	       $this->Categoria->create();
           if($this->Categoria->save($this->request->data)){
                $this->Session->setFlash('Categoria registrado con exito','msgbueno');
                $this->redirect(array('action'=>'index',null, true));
           }
           else{
            $this->Session->setFlash('No se pudo registrar la Categoria');
            
           }
	   }
	}

/**
 * edit method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null)
    {
        $this->Categoria->id = $id;
        if (!$id) {
            $this->Session->setFlash('No existe la Categoria');
            $this->redirect(array('action' => 'index'), null, true);
        }
        if (empty($this->request->data)) {
            $this->request->data = $this->Categoria->read();
        } else {
            if ($this->Categoria->save($this->request->data)) {
                $this->Session->setFlash('Los datos fueron modificados', 'msgbueno');
                $this->redirect(array('action' => 'index'), null, true);
            } else {
                $this->Session->setFlash('no se pudo modificar!!');
            }
        }
    }

/**
 * delete method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function delete($id = null)
    {
        $this->Categoria->id = $id;
        $this->request->data = $this->Categoria->read();
        if (!$id) {
            $this->Session->setFlash('No existe la Categoria a Eliminar','msgbueno');
            $this->redirect(array('action' => 'index'));
        } else {
            if ($this->Categoria->delete($id)) {
                $this->Session->setFlash('Se elimino la Categoria ' . $this->data['Categoria']['nombres'],'msgbueno');
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('Error al eliminar','msgbueno');
            }
        }
    }
    
    public function ajaxedita($idcategoria = null){
        $this->layout = 'ajax';
        $this->Categoria->id = $idcategoria;
        $this->data = $this->Categoria->read();
        $this->set(compact(idcategoria));
    }
 }
